<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="handleInputValue"/>
    <a-button type="primary" @click="addItemToList">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="cdStatusChanged($event,item.id)">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItemById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div class="footer" slot="footer"> 
        <span>{{getReduce}}条剩余</span>
        <a-button-group>
          <a-button :type="viewKey =='all'? 'primary' : 'default' " @click="changeList('all')">全部</a-button>
          <a-button :type="viewKey =='unDone'? 'primary' : 'default' " @click="changeList('unDone')">未完成</a-button>
          <a-button :type="viewKey =='done'? 'primary' : 'default' " @click="changeList('done')">已完成</a-button>   
        </a-button-group>
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>
<script>
import { mapMutations,mapActions,mapState, mapGetters } from 'vuex';
export default {
  name: "app",
  data() {
    return {
    };
  },
  computed:{
    ...mapState(['inputValue','viewKey']),
    ...mapGetters(['getReduce','infoList'])
  },
  created(){
    this.getList()
  },
  methods:{
    ...mapMutations(['setInputValue','addItem','removeItem','changeStatus','cleanDone','changeViewKey']),
    ...mapActions(['getList']),
    //监听文本框内容变化
    handleInputValue(e){     
        this.setInputValue(e.target.value)
    },
    //添加item项
    addItemToList(){
      if(this.inputValue.trim().length <= 0){
        return this.$message.warning('文本框不能为空！')
      }
      this.addItem()
    },
    //删除列表项
    removeItemById(id){
      this.removeItem(id)
    },
    //监听列表项是否完成
    cdStatusChanged(e,id){ 
      const obj = {
        id,
        status:e.target.checked
      }
      this.changeStatus(obj)
    },
    //清除已完成
    clean(){
      this.cleanDone()
    },
    //改变页面上展示的列表数据
    changeList(type){
      this.changeViewKey(type)
    }

  }
  
};
</script>
<style scoped>
#app {
  padding: 10px;
}
.my_ipt {
  width: 500px;
  margin-right: 10px;
}
.dt_list {
  width: 500px;
  margin-top: 10px;
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
